<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

session_start();

class Home extends CI_Controller {
    public function index() {
        if(isset($_SESSION['user'])) {
            redirect(base_url().'home/notifications','refresh');
        } else {
            redirect(base_url().'account/login?err=3','refresh');
        }
    }
    
    public function design()
    {
            $this->template->set_partial('styles','partials/styles');
            $this->template->set_partial('scripts_a','partials/scripts_a');
            $this->template->set_partial('header','partials/header');
            $this->template->set_partial('subnav','partials/subnav');
            $this->template->set_partial('search','partials/search');
            $this->template->set_partial('navigation','partials/navigation');
            $this->template->set_partial('scripts_b','partials/scripts_b');
            $this->template->set_partial('footer','partials/footer');
    }
    
    public function friendRequest() {
        if($this->session->userdata('username')){
            if(!isset($_GET['page'])) {
                redirect(base_url().'home/friendRequest?page=1','refresh');
            }
            
            $select = array('account_id','f_name','l_name','about_me','avatar');
            $table = array('tbl_account','tbl_profile','tbl_friend');
            $where[0] = array('tbl_profile.profile_id','=','tbl_account.profile_id',' AND ');
            $where[1] = array('tbl_account.account_id','=','tbl_friend.UserA',' AND ');
            $where[2] = array('UserB','=',$this->session->userdata('account_id'),' AND ');
            $where[3] = array('tbl_friend.status','=',0,' AND ');
            $sortby = 'friend_id';
            $sorttype = 'DESC';
            
            $this->load->model('msearch');
            $search = $this->msearch->search($select,$table,$where,$sortby,$sorttype,$_GET['page'],5);
            $data['friendrequest'] = $search['result'];
            $data['totalpage'] = $search['totalpage'];
            
            $this->template->set_layout('orpms');
            $this->template->title('Friend Request - ORPMS');
            $this->design();
            $this->template->build('pages/home/friendrequest',$data);            
            /*
            $this->load->model('mfriend');
            $data['friendrequest'] = $this->mfriend->friendRequest($this->session->userdata('account_id'));
                
            $this->template->set_layout('orpms');
            $this->template->title('Friend Request - ORPMS');
            $this->design();
            //Build
            $this->template->build('pages/friendrequest',$data);
            */
        } else {
            redirect(base_url().'account/login','refresh');
        }
    }
    
    public function notifications()
    {
        if($this->session->userdata('username')) {
            $this->template->set_layout('orpms');
            $this->template->title('Notifications - ORPMS');
            $this->design();
            //Build
            $this->template->build('pages/home/notifications');
        } else {
            redirect(base_url().'account/login','refresh');
        }
    }
}